home *** CD-ROM | disk | FTP | other *** search
/ Language/OS - Multiplatform Resource Library / LANGUAGE OS.iso / cpp_libs / nihcl-30.lha / nihcl-3.0 / ex / ex13-8.c < prev    next >
C/C++ Source or Header  |  1990-05-15  |  5KB  |  215 lines

  1. // ex13-8.c -- Static method for avoiding undesired multiple calls
  2. //             to member functions of virtual base classes
  3.  
  4. // $Header: /afs/alw.nih.gov/unix/sun4_40c/usr/local/src/nihcl-3.0/share/ex/RCS/ex13-8.c,v 3.0 90/05/15 22:44:49 kgorlen Rel $
  5.  
  6. #include <stdio.h>
  7. #include <iostream.h>
  8.  
  9. class Link;
  10.  
  11. class LinkedList {
  12.     Link* firstLink;    // pointer to first Link of list 
  13.     Link* lastLink;     // pointer to last Link of list 
  14. public:
  15.     LinkedList()        { firstLink = lastLink = 0; }
  16.     Link* add(Link& l);
  17.     virtual void printOn(ostream& strm =cout) const;
  18. };
  19.  
  20. class Link {
  21.     Link* next;
  22.     friend LinkedList;
  23. public:
  24.     Link()                  { next = 0; }
  25.     Link* nextLink() const  { return next; }
  26.     virtual void printOn(ostream& strm =cout) const =0;
  27. };
  28.  
  29. Link* LinkedList::add(Link& l)
  30. {
  31.     if (firstLink == 0) firstLink = lastLink = &l;
  32.     else {
  33.         lastLink->next = &l;
  34.         lastLink = &l;
  35.     }
  36.     return &l;
  37. }
  38.  
  39. void LinkedList::printOn(ostream& strm) const
  40. {
  41.     int n = 0;
  42.     Link* l = firstLink;
  43.     while (l != 0) {
  44.         if (n++ != 0) strm << endl;
  45.         l->printOn(strm);
  46.         l = l->next;
  47.     }
  48. }
  49.  
  50. //--------------------------------------------
  51.  
  52. class AllLink: public Link {
  53. protected:
  54.     AllLink() {};
  55.     virtual void printOn(ostream& strm =cout) const =0;
  56. };
  57.  
  58. class QLink: public Link {
  59. protected:
  60.     QLink() {};
  61.     virtual void printOn(ostream& strm =cout) const =0;
  62. };
  63.  
  64. class AllVehicles: public LinkedList {
  65. public:
  66.     virtual void addVehicle(AllLink&);
  67. };
  68.  
  69. void AllVehicles::addVehicle(AllLink& l)    { add(l); }
  70.  
  71. class VehicleQ: public LinkedList {
  72. public:
  73.     virtual void addVehicle(QLink&);
  74. };
  75.  
  76. void VehicleQ::addVehicle(QLink& l)     { add(l); }
  77.  
  78. class Vehicle: public AllLink, public QLink {
  79.     static unsigned vehicleID;
  80.     static AllVehicles allVehicles;
  81.     unsigned id;
  82.     float height;
  83.     float length;
  84. protected:
  85.     Vehicle(float h = 0.0, float l = 0.0) {
  86.         id = ++vehicleID;
  87.         height = h; length = l;
  88.         allVehicles.addVehicle(*this);
  89.     }
  90.     virtual void _printOn(ostream& strm =cout) const;
  91. public:
  92.     static void printAll(ostream& strm =cout) {
  93.         allVehicles.printOn(strm);
  94.     }
  95.     virtual void printOn(ostream& strm =cout) const;
  96. // ...
  97. };
  98.  
  99. unsigned Vehicle::vehicleID = 0;
  100. AllVehicles Vehicle::allVehicles;
  101.  
  102. void Vehicle::_printOn(ostream& strm) const
  103. {
  104.     strm << '#' << id << " height " << height << "  length "
  105.         << length;
  106. }
  107.  
  108. void Vehicle::printOn(ostream& strm) const
  109. {
  110.     _printOn(strm);
  111. }
  112.  
  113. class LandVhcl: public virtual Vehicle {
  114.     unsigned axles;
  115. protected:
  116.     virtual void _printOn(ostream& strm =cout) const;
  117. public:
  118.     LandVhcl(float h, float l, unsigned a =2) : Vehicle(h,l)
  119.         { axles = a; }
  120.     virtual void printOn(ostream& strm =cout) const;
  121. // ...
  122. };
  123.  
  124. void LandVhcl::_printOn(ostream& strm) const
  125. {
  126.     strm << "  axles " << axles;
  127. }
  128.  
  129. void LandVhcl::printOn(ostream& strm) const
  130. {
  131.     Vehicle::_printOn(strm);
  132.     _printOn(strm);
  133. }
  134.  
  135. class WaterVhcl: public virtual Vehicle {
  136.     float draft;
  137. protected:
  138.     virtual void _printOn(ostream& strm =cout) const;
  139. public:
  140.     WaterVhcl(float h, float l, float d) : Vehicle(h,l)
  141.         { draft = d; }
  142.     virtual void printOn(ostream& strm =cout) const;
  143. // ...
  144. };
  145.  
  146. void WaterVhcl::_printOn(ostream& strm) const
  147. {
  148.     strm << "  draft " << draft;
  149. }
  150.  
  151. void WaterVhcl::printOn(ostream& strm) const
  152. {
  153.     Vehicle::_printOn(strm);
  154.     _printOn(strm);
  155. }
  156.  
  157. class AmphibVhcl: public LandVhcl, public WaterVhcl {
  158. protected:
  159.     virtual void _printOn(ostream& strm =cout) const;
  160. public:
  161.     AmphibVhcl(float h, float l, float d, unsigned a=2)
  162.         : Vehicle(h,l), LandVhcl(h,l,a), WaterVhcl(h,l,d) {}
  163.     virtual void printOn(ostream& strm =cout) const;
  164. // ...
  165. };
  166.  
  167. void AmphibVhcl::_printOn(ostream& strm) const {}
  168.  
  169. void AmphibVhcl::printOn(ostream& strm) const
  170. {
  171.     Vehicle::_printOn(strm);
  172.     LandVhcl::_printOn(strm);
  173.     WaterVhcl::_printOn(strm);
  174.     _printOn(strm);
  175. }
  176.  
  177. class StopLightQ: public VehicleQ {
  178. public:
  179.     virtual void addVehicle(LandVhcl&);
  180. };
  181.  
  182. void StopLightQ::addVehicle(LandVhcl& v)
  183.    { VehicleQ::addVehicle(v); }
  184.  
  185. class DrawBridgeQ: public VehicleQ {
  186. public:
  187.     virtual void addVehicle(WaterVhcl&);
  188. };
  189.  
  190. void DrawBridgeQ::addVehicle(WaterVhcl& v)
  191.    { VehicleQ::addVehicle(v); }
  192.  
  193. StopLightQ stopLightQ[2];
  194. DrawBridgeQ drawBridgeQ;
  195.  
  196. main()
  197. {
  198.     stopLightQ[0].addVehicle(*new LandVhcl(4.1, 12.0));
  199.     stopLightQ[1].addVehicle(*new LandVhcl(4.2, 12.0));
  200.     stopLightQ[0].addVehicle(*new LandVhcl(4.3, 12.0));
  201.     stopLightQ[1].addVehicle(*new LandVhcl(4.4, 12.0));
  202.     drawBridgeQ.addVehicle(*new WaterVhcl(21.0, 19.0, 3.5));
  203.     drawBridgeQ.addVehicle(*new WaterVhcl(10.0, 30.0, 2.0));
  204.     stopLightQ[0].addVehicle(*new AmphibVhcl(5.0, 15.0, 3.0));
  205.     drawBridgeQ.addVehicle(*new AmphibVhcl(5.1, 15.0, 3.0));
  206.     cout << "allVehicles:\n"; Vehicle::printAll();
  207.     cout << endl;
  208.     cout << "stopLightQ[0]:\n"; stopLightQ[0].printOn();
  209.     cout << endl;
  210.     cout << "stopLightQ[1]:\n"; stopLightQ[1].printOn();
  211.     cout << endl;
  212.     cout << "drawBridgeQ:\n"; drawBridgeQ.printOn();
  213.     cout << endl;
  214. }
  215.